package com.incar.chuxing.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.incar.chuxing.utils.Logger;

/**
 * Created by Michael on 11/27.
 */

public class CountDownIndicate extends View {


    private Paint bgLinePaint;
    private Paint linePaint;
    private Paint dotPaint;
    private Path path;
    private int strokeWidth;//线宽
    private int dotRadius;//圆点的半径
    private int progress;//进度百分数
    private float circlePerimeter;//单个半圆的长
    private float lineLength;//单条直线的长
    private float totalLength; //圈的总长

    public CountDownIndicate(Context context) {
        super(context);
        initPaint();
    }

    public CountDownIndicate(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    //初始化画笔 
    private void initPaint() {

        path = new Path();
        bgLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        bgLinePaint.setStyle(Paint.Style.STROKE);

        linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        linePaint.setStyle(Paint.Style.STROKE);

        dotPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        dotPaint.setStyle(Paint.Style.FILL);

        dotPaint.setColor(Color.parseColor("#296CFF"));
        bgLinePaint.setColor(Color.parseColor("#F8F8FA"));
        linePaint.setColor(Color.parseColor("#296CFF"));

        strokeWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, getResources().getDisplayMetrics());
        bgLinePaint.setStrokeWidth(strokeWidth);
        linePaint.setStrokeWidth(strokeWidth);

        dotRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 6, getResources().getDisplayMetrics());

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // 将sp转为px  applyDimension只能把其他转为px
        int w = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 159, getResources().getDisplayMetrics());
        int h = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 69, getResources().getDisplayMetrics());
        setMeasuredDimension(w, h);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        circlePerimeter = (float) (Math.PI * (getHeight() / 2 - dotRadius));
        lineLength = getWidth() - getHeight();
        totalLength = (circlePerimeter + lineLength) * 2;
        bgLinePaint.setColor(Color.parseColor("#F8F8FA"));
        path.moveTo(getHeight() / 2, dotRadius);
        //画上直线
        path.lineTo(getWidth() / 2, dotRadius);
        //画右曲线
        RectF mRectFRight = new RectF(getWidth() - getHeight() + dotRadius, dotRadius, getWidth() - dotRadius, getHeight() - dotRadius);
        path.arcTo(mRectFRight, -90, 180);
        //画下直线
        path.moveTo(getWidth() - getHeight() / 2, getHeight() - dotRadius);
        path.lineTo(getHeight() / 2, getHeight() - dotRadius);
        //画左曲线
        RectF mRectFLeft = new RectF(dotRadius, dotRadius, getHeight() - dotRadius, getHeight() - dotRadius);
        path.arcTo(mRectFLeft, 90, 180);
        canvas.drawPath(path, bgLinePaint);


        StopPosition stopPosition = getStopPosition(progress);
        switch (stopPosition.direction) {
            case TOP_LEFT:
                //画上左直线
                path.reset();
                path.moveTo(getWidth() / 2, dotRadius);
                path.lineTo(getWidth() - getHeight() / 2, dotRadius);

                path.arcTo(
                        new RectF(getWidth() - getHeight() + dotRadius, dotRadius, getWidth() - dotRadius, getHeight() - dotRadius),
                        -90, 180);
                path.moveTo(getWidth() - getHeight() / 2, getHeight() - dotRadius);
                path.lineTo(getHeight() / 2, getHeight() - dotRadius);
                path.arcTo(new RectF(dotRadius, dotRadius, getHeight() - dotRadius, getHeight() - dotRadius), 90, 180);

                path.moveTo(getHeight() / 2, dotRadius);
                path.lineTo(getHeight() / 2 + stopPosition.position, dotRadius);
                canvas.drawPath(path, linePaint);


                canvas.drawCircle(getHeight() / 2 + stopPosition.position, dotRadius, dotRadius, dotPaint);
                break;
            case LEFT: {
                //画左曲线
                path.reset();
                path.moveTo(getWidth() / 2, dotRadius);
                path.lineTo(getWidth() - getHeight() / 2, dotRadius);

                path.arcTo(
                        new RectF(getWidth() - getHeight() + dotRadius, dotRadius, getWidth() - dotRadius, getHeight() - dotRadius),
                        -90, 180);

                path.moveTo(getWidth() - getHeight() / 2, getHeight() - dotRadius);
                path.lineTo(getHeight() / 2, getHeight() - dotRadius);
                //画左曲线
                path.arcTo(new RectF(dotRadius, dotRadius, getHeight() - dotRadius, getHeight() - dotRadius),
                        90, stopPosition.position * 180);
                Logger.d("stopPosition.position", stopPosition.position + "");
                canvas.drawPath(path, linePaint);


                float dotX = dotRadius + (getHeight() / 2 - dotRadius) * (1 - (float) Math.sin(stopPosition.position * Math.PI));

                float dotY = getHeight() / 2 + (getHeight() / 2 - dotRadius) * (float) Math.cos(stopPosition.position * Math.PI);
                canvas.drawCircle(dotX, dotY, dotRadius, dotPaint);
            }
            break;
            case BOTTOM:
                //画下直线
                path.reset();
                path.moveTo(getWidth() / 2, dotRadius);
                path.lineTo(getWidth() - getHeight() / 2, dotRadius);

                path.arcTo(
                        new RectF(getWidth() - getHeight() + dotRadius, dotRadius, getWidth() - dotRadius, getHeight() - dotRadius),
                        -90, 180);
                canvas.drawPath(path, linePaint);

                path.moveTo(getWidth() - getHeight() / 2, getHeight() - dotRadius);
                path.lineTo(getWidth() - getHeight() / 2 - stopPosition.position, getHeight() - dotRadius);
                canvas.drawPath(path, linePaint);
                canvas.drawCircle(getWidth() - getHeight() / 2 - stopPosition.position, getHeight() - dotRadius, dotRadius, dotPaint);

                break;
            case RIGHT:
                //画右曲线
                path.reset();
                path.moveTo(getWidth() / 2, dotRadius);
                path.lineTo(getWidth() - getHeight() / 2, dotRadius);

                path.arcTo(
                        new RectF(getWidth() - getHeight() + dotRadius, dotRadius, getWidth() - dotRadius, getHeight() - dotRadius),
                        -90, stopPosition.position * 180);
                Logger.d("swwepAngle", stopPosition.position * 180 + "");
                canvas.drawPath(path, linePaint);

                float dotX = getHeight() / 2 + lineLength + (float) Math.sin(stopPosition.position * Math.PI) * (getHeight() / 2 - dotRadius);
                float dotY = getHeight() / 2 - (float) Math.cos(stopPosition.position * Math.PI) * (getHeight() / 2 - dotRadius);
                canvas.drawCircle(dotX, dotY, dotRadius, dotPaint);
                break;
            case TOP_RIGHT:
                //画上右直线
                path.reset();
                path.moveTo(getWidth() / 2, dotRadius);
                path.lineTo(getWidth() / 2 + stopPosition.position, dotRadius);
                canvas.drawPath(path, linePaint);
                canvas.drawCircle(getWidth() / 2 + stopPosition.position, dotRadius, dotRadius, dotPaint);
                break;


        }
    }

    public void setProgress(int progress) {
        if (progress > 100)
            progress = 100;
        if (this.progress != progress) {
            invalidate();
        }
        this.progress = progress;
    }

    public StopPosition getStopPosition(int progress) {
        StopPosition stopPosition = new StopPosition();
        float currentLength = (float) progress / 100 * totalLength;
        Logger.d("curLengthTo", totalLength + "");
        if (currentLength > totalLength - lineLength / 2) {
            stopPosition.position = currentLength - totalLength + lineLength / 2;
            stopPosition.direction = Direction.TOP_LEFT;

        } else if (currentLength > totalLength - lineLength / 2 - circlePerimeter) {
            stopPosition.position = (currentLength - (totalLength - lineLength / 2 - circlePerimeter)) / circlePerimeter;
            stopPosition.direction = Direction.LEFT;
        } else if (currentLength > totalLength - lineLength / 2 - circlePerimeter - lineLength) {
            stopPosition.position = currentLength - (totalLength - lineLength / 2 - circlePerimeter - lineLength);
            stopPosition.direction = Direction.BOTTOM;
        } else if (currentLength > lineLength / 2) {
            stopPosition.position = (currentLength - lineLength / 2) / circlePerimeter;
            Logger.d("stopPosition", stopPosition.position + "");
            stopPosition.direction = Direction.RIGHT;
        } else if (currentLength >= 0) {

            stopPosition.position = currentLength;
            stopPosition.direction = Direction.TOP_RIGHT;
        }

        return stopPosition;
    }

    private class StopPosition {

        private Direction direction;
        private float position;

        public StopPosition() {

        }
    }

    public enum Direction {
        TOP_LEFT, TOP_RIGHT, RIGHT, BOTTOM, LEFT
    }
}
